/**
 * 
 */
package com.hu.ta.assignment6.io;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
 * cage@g.com
 */
public class FileResult extends FileIO {

	private static final long serialVersionUID = -8642725760070373293L;

	private FileEmail[] resultlist;

	/**
	 * @param path
	 */
	public FileResult(String path) {
		super(path);
	}

	public void setResultList(FileEmail[] result) {
		resultlist = result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hu.ta.assignment6.io.FileIO#read()
	 */
	@Override
	public boolean read() {
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hu.ta.assignment6.io.FileIO#write()
	 */
	@Override
	public boolean write() {
		BufferedWriter writer = null;
		boolean result = false;
		try {
			writer = new BufferedWriter(new FileWriter(this));

			for (FileEmail e : resultlist) {
				writer.write(String.format("%s\t%s\n", e.getName(), (e
						.getClassification().isSpam() ? "SPAM" : "NOSPAM")));
			}

			result = true;
		} catch (IOException e) {
			e.printStackTrace();

		} finally {
			if (writer != null)
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}

		return result;
	}

}
